package com.csu.bailan.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.csu.bailan.domain.Cart;
import com.csu.bailan.domain.CartItem;
import com.csu.bailan.domain.SKU;
import com.csu.bailan.persistence.mapper.CartItemMapper;
import com.csu.bailan.persistence.mapper.CartMapper;
import com.csu.bailan.persistence.mapper.SKUMapper;
import com.csu.bailan.service.CartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;

@Service
@Slf4j
public class CartServiceImpl implements CartService {

    @Autowired
    private CartMapper cartMapper;
    @Autowired
    private CartItemMapper cartItemMapper;
    @Autowired
    private SKUMapper skuMapper;

    @Override
    public Cart getCartByUserId(String userId) {
        return cartMapper.selectOne(new QueryWrapper<Cart>().eq("user_id", userId));
    }

    @Override
    public List<CartItem> getCIByCartId(String cartId) {
        return cartItemMapper.selectList(new QueryWrapper<CartItem>().eq("cart_id", cartId));
    }

    @Override
    public boolean createCart(String userId) {
        Cart cart = cartMapper.selectOne(new QueryWrapper<Cart>().eq("user_id", userId));
        if(cart==null){
            Cart newcart = new Cart();
            newcart.setToPay((double) 0);
            newcart.setTotalPrice((double) 0);
            newcart.setUserId(userId);
            newcart.setCartStatus(0);
            int cc = cartMapper.insert(newcart);
            if (cc>0){
                log.info("创建购物袋成功");
                return true;
            }
            else {
                log.error("创建购物袋失败");
                return false;
            }
        }
        return true;
    }

    @Override
    public boolean deleteCartItem(Set<String> ciIds) {
        int deleted = cartItemMapper.deleteBatchIds(ciIds);
        if (deleted > 0){
            log.info("删除"+deleted+"条记录");
            return true;
        }
        else return false;
    }
    @Override
    public boolean addtocart(String skuId,String userId,int amount){
        SKU sku = skuMapper.selectById(skuId);
        Cart cart = cartMapper.selectOne(new QueryWrapper<Cart>().eq("user_id", userId));
        cart.setToPay(cart.getToPay()+sku.getSkuSales());
        cart.setTotalPrice(cart.getTotalPrice()+sku.getPrice());
        int cart_update_num = cartMapper.updateById(cart);
        if(cart_update_num<=0){
            return false;
        }
        CartItem cartItem = new CartItem();
        cartItem.setCitemId(userId+skuId);
        cartItem.setCartId(cart.getCartId());
        cartItem.setSkuId(skuId);
        cartItem.setAmount(amount);
        cartItem.setPrice(sku.getPrice());
        cartItem.setSalePrice(Double.valueOf(sku.getSkuSales()));
        cartItem.setCitemStatus(0);
        int cartitem_update_num = cartItemMapper.insert(cartItem);
        if(cartitem_update_num<=0){
            return false;
        }
        return true;
    }
}
